<?php

namespace app\data\controller\activity;

use app\data\controller\base\Publics;
use think\Db;

/**
 * 活动参与
 * Class
 * @package app\data\controller\base
 */
class Takes
{

	use \app\common\traits\ToolTrait;
	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 表默认配置
	 * @var \app\data\constants\TabConstants::TAb_CONFIG
	 */
	protected $tabConfig = null;

	/**
	 * 翻页存储路径
	 * @var string
	 */
	protected $path = "./timer/page/activity";

	/**
	 * 翻页文件存储配置
	 * @var string
	 */
	protected  $fileConfig = [
		"regs" => "reg_page.txt" 	//  新人注册交易达标
	];

	/**
	 * 翻页配置
	 * @var array
	 */
	protected $pageConfig = [
		1 	=> ["page" 	=> 0, "total"	=> 0, "limit" => 2]
	];

	/**
	 * Activity模型对象
	 * @var \app\common\model\Activity
	 */
	protected $model = null;

	/**
	 * 构造
	 */
	public function __construct()
	{
		$this->tabConfig = Publics::instance()->tab();
		$this->model = new \app\common\model\ActivityTake;
	}

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 临时存储
	 */
	public function temyStore($typs)
	{
		$ids = $this->get_ids($typs);
		if ($ids) {
			$data = [];
			foreach ($ids as $v) {
				$temp = [];
				$temp["actid"] = $v;
				array_push($data, $temp);
			}
			$check = Db::name($this->tabConfig->act_temy)->count();
			if (!$check) {
				$ret = Db::name($this->tabConfig->act_temy)->insertAll($data);
				if ($ret) {
					$file = $this->fileConfig["regs"];
					$pageConfig = $this->pageConfig[$typs];
					$this->create_directory($this->path, $file, $pageConfig);
					$totalPage = 0;
					$total = count($ids);
					if ($total) {
						$totalPage = ceil($total / $pageConfig['limit']);
					}
					$pageConfig["total"] = $totalPage;
					file_put_contents($this->path . '/' . $file, serialize($pageConfig));
				}
			}
		}
	}

	/**
	 * 达标计算
	 */
	public function standsCalc($id)
	{
		if (!$id) {
			return false;
		}
		$take = $this->_getTake($id);
		if ($take) {
			$tranAmt = $this->_getCustomerInteTransTotal($take["uid"], $take["start"], $take["ends"]);
			$tranAmt = 1000000;
			if (is_numeric($tranAmt)) {
				$update = [];
				$update = ["tranAmt" => $tranAmt];
				if (intval($tranAmt) >= intval($take["standsAmt"])) {
					$update["is_stands"] = 1;
					$update["stands_time"] = strtotime(date("Y-m-d", time()));
				}
				$ret = $this->model->where("id", $id)->update($update);
				if ($ret) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 获取客户交易某区间端交易总和
	 */
	public function _getCustomerInteTransTotal($uid, $start, $ends)
	{
		$sum = 0;
		if ($uid && $start && $ends) {
			$sum = Db::name($this->tabConfig->cus_tran_day)
				->where("uid", $uid)
				->where("times >= {$start} and times <= {$ends}")
				->sum("money");
		}
		return $sum;
	}

	/**
	 * 获取参与记录
	 */
	public function _getTake($id)
	{
		return $this->model
			->where("id", $id)
			->field("uid,active_typs,start,ends,tranAmt,standsAmt,is_stands,act_id")
			->find()
			->toArray();
	}

	/**
	 * 创建文件
	 */
	public function create_directory($path, $file, $pageCon)
	{
		$isDir = $this->creDirectory($path);
		if ($isDir) {
			$file = $path . "/" .  $file;
			!file_exists($file) && file_put_contents($file, serialize($pageCon));
		}
	}

	/**
	 * 清空表
	 */
	public function clearTable()
	{
		Db::execute("TRUNCATE `fd_community`.`" . $this->tabConfig->prefix . $this->tabConfig->act_temy . "`");
	}

	/**
	 * 文件路径
	 */
	public function file_path($t)
	{
		return $this->path . "/" . $this->fileConfig[$t];
	}

	/**
	 * 获取参与ID 组
	 */
	public function get_ids($typs)
	{
		if (!$typs) {
			return false;
		}
		$ret = Db::name($this->tabConfig->take)->where("active_typs", $typs)->where("is_stands", 2)->column("id");
		return $ret;
	}
}
